package com.titan.core.utils.object;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

import com.titan.core.base.BaseVariant;
import com.titan.core.utils.data.FormatUtils;

/**
 * @author 周成功
 * @email 86898837@qq.com
 * @date 2020年02月14日
 */
public class Variant<T> extends BaseVariant {
	private T value = null;
	
    public Variant() {
    }
	public Variant(T value) {
		this.value = value;
	}
    public static <T> Variant<T> $() {
        return new Variant<T>();
    }
    public static <T> Variant<T> $(T value) {
        return new Variant<T>(value);
    }
    

	@Override
	public int size() {
		return 1;
	}
    @Override
    public String[] getVariants() {
        return new String[]{"value"};
    }
    @Override
    public Object[] getValues() {
        return new Object[]{this.getValue()};
    }    
    @Override
    public Object getValue(int index) {
        return this.getValue();
    }
    @Override
    public void setValue(int index, Object value) {
        this.value = (T)value;
    }
    @Override
    public void clear() {
        this.value = null;      
    }    
    
	// 设置属性
	public Variant<T> setValue(T value) {
		this.value = value;
		return this;
	}
	// 获得属性值
	public T getValue() {
		return this.value;
	}
	
	public String toString() {
	    try {
            return getString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
	}
	public String getString() {
	    return FormatUtils.toString(value);
	}
    public String getString(String pattern) {
        return FormatUtils.toString(value, pattern);
    }
    /*
	public Byte getByte() {
	    return FormatUtils.toByte(value);
	}
    public Character getCharacter() {
        return ToUtils.toCharacter(value);
    }
	public Short getShort() {
	    return ToUtils.toShort(value);
	}
	public Integer getInteger() {
	    return ToUtils.toInteger(value);
	}
	*/
	public Long getLong() {
	    return FormatUtils.toLong(value);
	}
    public Float getFloat() {
        return FormatUtils.toFloat(value);
    }
    public Double getDouble() {
        return FormatUtils.toDouble(value);
    }
	/*
	public BigDecimal getBigDecimal() {
	    return ToUtils.toBigDecimal(value);
	}
	*/
	public Boolean getBoolean() {
	    return FormatUtils.toBoolean(value);
	}
    public Date getDate() {
        return FormatUtils.toDate(value);
    }
	/*
    public byte[] getByteArray() throws IOException {
        return ToUtils.toByteArray(value, "UTF-8");
    }
    public byte[] getByteArray(String encoding) throws IOException {
        return ToUtils.toByteArray(value, encoding);
    }
    */
}
